package com.customization.huangwm.formmode.osahc;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.axis.components.logger.LogFactory;
import org.apache.commons.logging.Log;

import weaver.conn.RecordSet;
import weaver.general.Util;

/**
 * @author Huangwm
 * @package com.customization.huangwm.formmode.osahc
 * @description
 */
public class OsahcSendMsgUtil {
    private Log log = new LogFactory().getLog("custom_huangwm");

    public String[] main_fields = new String[]{"id", "sfyfstx", "nf", "txr", "bm", "rq", "fj"};

    public String[] detail_fields = new String[]{"id", "mainid", "x", "mb", "hlzb", "hzgspfjcf", "hzgspfjf", "swyypfjcf"
            , "swyypfjf", "fzr", "bm", "lc"
            , "jtgjnr", "jd", "dyctxrq", "dectxrq", "dsctxrq", "zt"};

    public String detail_x = "x";
    public String detail_mb = "mb";
    public String detail_hlzb = "hlzb";
    public String detail_hzgspfjcf = "hzgspfjcf";
    public String detail_hzgspfjf = "hzgspfjf";
    public String detail_swyypfjcf = "swyypfjcf";
    public String detail_swyypfjf = "swyypfjf";
    public String detail_fzr = "fzr";
    public String detail_bm = "bm";
    public String detail_lc = "lc";
    public String detail_jtgjnr = "jtgjnr";
    public String detail_jd = "jd";
    public String detail_dyctxrq = "dyctxrq";
    public String detail_dectxrq = "dectxrq";
    public String detail_dsctxrq = "dsctxrq";
    public String detail_zt = "zt";


    /**
     * 根据id查询建模主表信息
     *
     * @param table
     * @param billid
     * @return
     */
    public Map<String, String> getMaintableInfo(String table, String billid) throws SQLException {
        Map<String, String> result = new HashMap<String, String>();
        RecordSet rs = new RecordSet();
        String sql = "select * from " + table + " where id=" + billid;
        rs.execute(sql);
        log.info("OsahcSendMsgUtil 查询主表数据：" + sql);
        if (rs.next()) {
            for (int i = 0; i < main_fields.length; i++) {
                result.put(main_fields[i], Util.null2String(rs.getString(main_fields[i])));
            }
        } else {
            result.put("queryResult", "0");
        }
        log.info(result);
        log.info("主表查询完成");
        return result;
    }

    /**
     * 根据主表id查询明细数据
     *
     * @param table
     * @param detailIndex
     * @param billid
     * @return
     */
    public List<Map<String, String>> getDetailtableInfo(String table, String detailIndex, String billid) throws SQLException {
        List<Map<String, String>> result = new ArrayList<Map<String, String>>();
        RecordSet rs = new RecordSet();
        String sql = "select * from " + table + "_dt" + detailIndex + " where mainid=" + billid + " and ((lc='' or lc is null) or (zt=1))";
        rs.execute(sql);
        log.info("OsahcSendMsgUtil 查询明细数据：" + sql);
        while (rs.next()) {
            Map<String, String> data = new HashMap<>();
            for (int i = 0; i < detail_fields.length; i++) {
                data.put(detail_fields[i], Util.null2String(rs.getString(detail_fields[i])));
            }
            result.add(data);
        }
        log.info(result);
        log.info("明细表查询完成");
        return result;
    }

}
